package com.yh.threadTest.test;

import java.awt.*;

/**
 * @ClassName Demo6
 * @Description TODO
 * @Author 0715-YuHao
 * @Date 2020/11/11 14:54
 */
public class Demo6 {

    public static void main(String[] args) {
        //线程死锁
        Culprit c = new Culprit();
        Police p = new Police();
        new MyThread(c, p).start();
        c.say(p);
    }

    static class MyThread extends Thread {
        private Culprit c;
        private Police p;

        public MyThread() {}

        public MyThread(Culprit c, Police p) {
            this.c = c;
            this.p = p;
        }

        @Override
        public void run() {
            p.say(c);
        }
    }

    static class Culprit{
        public synchronized void say(Police p) {
            System.out.println("罪犯:你放了我，我放了人质");
            p.repo();
        }
        public synchronized void repo() {
            System.out.println("我走了，人质放了");
        }
    }

    static class Police{
        public synchronized void say(Culprit c) {
            System.out.println("警察:你放了人质，我放了你");
            c.repo();
        }
        public synchronized void repo() {
            System.out.println("救了人质，罪犯跑了");
        }
    }
}
